package cn.com.shopec.erp.multiChannel.dao.impl;

import java.math.BigInteger;
import java.util.List;

import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;
import cn.com.shopec.erp.common.orm.impl.HibernateEntityDao;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.StringUtil;
import cn.com.shopec.erp.multiChannel.dao.IChannelOrderItemDao;
import cn.com.shopec.erp.multiChannel.entity.ChannelOrderItem;
import cn.com.shopec.erp.multiChannel.vo.ChannelOrderVo;

@Repository
public class ChannelOrderItemDaoImpl extends HibernateEntityDao<ChannelOrderItem> implements IChannelOrderItemDao {

	@Override
	public PageFinder channelOrderItemPaged(ChannelOrderVo channelOrderVo, PageQuery pageQuery) {
		String params = "";
		if (channelOrderVo != null) {
			if (StringUtil.isExist(channelOrderVo.getChannelOrderId())) {
				params += " AND coi.channel_order_id ='" + channelOrderVo.getChannelOrderId() + "'";
			}
		}
		String querysql = "select coi.id,coi.channel_order_id channelOrderId,coi.item_num prodNo,coi.sku_id skuId,"
				+ "coi.sku_name prodName,coi.item_price price,coi.item_total total,p.prod_bar_code prodBarCode,"
				+ "GROUP_CONCAT(SVO.OPTION_NAME) as specName,p.commo_no commoNo,"
				+ "s.id supNo,s.sup_name supName,s.code supCode,s.code code "
				+ "from tbl_channel_order_item coi "
				+ "left join tbl_product p on coi.item_num = p.prod_no "
				+ "left join tbl_prod_supplier ps on coi.item_num=ps.prod_no " 
				+ "left join tbl_supplier_info s ON ps.supplier_no=s.code "
				+ "left join tbl_prod_spec_value psv on psv.prod_no = ps.prod_no "
				+ "left join tbl_spec_val_option svo on svo.option_no = psv.val_option_no "
				+ "where 1=1 "
				+ params;
		String countsql = "select count(*) from tbl_channel_order_item coi where 1=1 " + params;
		BigInteger count = (BigInteger) this.createSQLQuery(countsql).list().get(0);
		List list = getSession().createSQLQuery(querysql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP)
				.setFirstResult((pageQuery.getPage() - 1) * pageQuery.getPageSize())
				.setMaxResults(pageQuery.getPageSize()).list();
		PageFinder page = new PageFinder(pageQuery.getPage(), pageQuery.getPageSize(), count.intValue(), list);
		return page;
	}
}
